package com.coder.books.thinkinjava.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static com.coder.util.Print.println;

/**
 * 生产者与消费者
 * 练习25、26
 *
 * @author yuhushuan
 * @date 2020/9/4 08:13
 */
class Meal {
    private final int orderNum;

    public Meal(int orderNum) {
        this.orderNum = orderNum;
    }

    @Override
    public String toString() {
        return "Meal " + orderNum;
    }
}

// 服务员
class WaitPerson implements Runnable {
    private Restaurant restaurant;

    public WaitPerson(Restaurant r) {
        restaurant = r;
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                synchronized (this) {
                    while (restaurant.meal == null) {
                        wait();
                    }
                    println("WaitPerson got " + restaurant.meal);
                    synchronized (restaurant.chef) {
                        restaurant.meal = null;
                        restaurant.chef.notifyAll();
                    }
                    synchronized (restaurant.busBody) {
                        restaurant.needCleaned = true;
                        restaurant.busBody.notifyAll();
                    }
                }
            }
        } catch (InterruptedException e) {
            System.out.println("WaitPerson Interrupted");
        }

    }
}

// 厨师
class Chef implements Runnable {
    private Restaurant restaurant;
    private int count = 0;

    public Chef(Restaurant r) {
        restaurant = r;
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                synchronized (this) {
                    while (restaurant.meal != null) {
                        wait();
                    }
                }
                if (++count == 10) {
                    println("Out of food, closing");
                    restaurant.exec.shutdownNow();
                    return;
                }
                println("Order up");
                TimeUnit.MILLISECONDS.sleep(100);
                restaurant.meal = new Meal(count);
                synchronized (restaurant.waitPerson) {
                    restaurant.waitPerson.notifyAll();
                }
            }
        } catch (InterruptedException e) {
            println("Chef Interrupted");
        }
    }
}

// 清洁
class BusBody implements Runnable {
    private Restaurant restaurant;

    public BusBody(Restaurant r) {
        restaurant = r;
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                synchronized (this) {
                    while (!restaurant.needCleaned) {
                        wait();
                    }
                }
                restaurant.needCleaned = false;
                println("BusBody clean");
            }
        } catch (InterruptedException e) {
            println("BusBody Interrupted");
        }
    }
}

// 餐厅
public class Restaurant {
    Meal meal;
    boolean needCleaned;
    ExecutorService exec = Executors.newCachedThreadPool();
    WaitPerson waitPerson = new WaitPerson(this);
    Chef chef = new Chef(this);
    BusBody busBody = new BusBody(this);

    public Restaurant() {
        exec.execute(chef);
        exec.execute(waitPerson);
        exec.execute(busBody);
    }

    public static void main(String[] args) {
        new Restaurant();
    }
}
